﻿using Lincolnwood.Foundation.SermonLibrary.Data.NHibernate;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using NHibernate;
using Lincolnwood.Foundation.SermonLibrary.Data;
using Lincolnwood.Foundation.SermonLibrary;
using System.Collections.Generic;
using Lincolnwood.Foundation.Common.Data.NHibernate;
using Lincolnwood.Foundation.Common.Data;

namespace Lincolnwood.Foundation.SermonLibrary.Tests
{
    /// <summary>
    ///This is a test class for NHibernateSermonSeriesRepositoryTest and is intended
    ///to contain all NHibernateSermonSeriesRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class NHibernateSermonSeriesRepositoryTest
    {
        private TestContext testContextInstance;
        private NHibernateHelper _nHibernateHelper = new NHibernateHelper();
        private IUnitOfWork _unitOfWork;

        public NHibernateSermonSeriesRepositoryTest()
        {
            _nHibernateHelper.ConfigureWith(typeof(NHibernateSermonSeriesRepository).Assembly);
        }

        [TestInitialize]
        public void TestInitialize()
        {
            _nHibernateHelper.GenerateSchema();
            _unitOfWork = new NHibernateSermonLibraryUnitOfWork();
        }

        [TestMethod]
        public void Can_Generate_Schema()
        {
            _nHibernateHelper.GenerateSchema();
            _unitOfWork = new NHibernateSermonLibraryUnitOfWork();
        }
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Lincolnwood.Foundation.SermonLibrary.Data.ISermonSeriesRepository.FindById
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Lincolnwood.Foundation.SermonLibrary.dll")]
        public void FindByIdTest()
        {

            ISermonSeriesRepository testRepository = new NHibernateSermonSeriesRepository(_unitOfWork);
            SermonSeries testSermonSeries = new SermonSeries(
                "Test Sermon Series", 
                "Test sermon Series Description");

            testRepository.Add(testSermonSeries);

            var testSermonSeriesId = testSermonSeries.Id;

            _unitOfWork.Flush();
            

            SermonSeries actual;
            actual = testRepository.FindById(testSermonSeriesId);

            Assert.AreEqual(testSermonSeries, actual);
        }

        /// <summary>
        ///A test for Lincolnwood.Foundation.SermonLibrary.Data.ISermonSeriesRepository.Add
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Lincolnwood.Foundation.SermonLibrary.dll")]
        public void AddTest()
        {

            ISermonSeriesRepository testRepository = new NHibernateSermonSeriesRepository(_unitOfWork);
            SermonSeries testSermonSeries = new SermonSeries(
                "Test Sermon Series",
                "Test sermon Series Description");

            testRepository.Add(testSermonSeries);

            var testSermonSeriesId = testSermonSeries.Id;

            _unitOfWork.Flush();
        }

        /// <summary>
        ///A test for GetSermonsPaged
        ///</summary>
        [TestMethod()]
        public void GetSermonsPagedTest()
        {

            ISermonSeriesRepository testRepository = new NHibernateSermonSeriesRepository(_unitOfWork);

            SermonSeries testSermonSeries1 = new SermonSeries(
                "Test Sermon Series1",
                "Test sermon Series Description1");

            SermonSeries testSermonSeries2 = new SermonSeries(
                "Test Sermon Series2",
                "Test sermon Series Description2");

            testRepository.Add(testSermonSeries1);
            testRepository.Add(testSermonSeries2);

            _unitOfWork.Flush();


            var pagedActual = testRepository.GetSermonsSeriesPaged(1, 2);

            Assert.AreEqual(2, pagedActual.Count);
        }


        /// <summary>
        ///A test for Lincolnwood.Foundation.SermonLibrary.Data.ISermonSeriesRepository.RemoveById
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Lincolnwood.Foundation.SermonLibrary.dll")]
        public void RemoveByIdTest()
        {

            ISermonSeriesRepository testRepository = new NHibernateSermonSeriesRepository(_unitOfWork);
            SermonSeries testSermonSeries = new SermonSeries(
                "Test Sermon Series",
                "Test sermon Series Description");

            testRepository.Add(testSermonSeries);

            var testSermonSeriesId = testSermonSeries.Id;

            _unitOfWork.Flush();


            SermonSeries actual;

            actual = testRepository.FindById(testSermonSeriesId);

            Assert.AreEqual(testSermonSeries, actual);


            testRepository.RemoveById(testSermonSeriesId);

            var shouldBeNull = testRepository.FindById(testSermonSeriesId);

            Assert.IsNull(shouldBeNull);
        }
    }
}
